#!/usr/bin/env python
'''
Created on Jan 21, 2011

@author: dexen
'''

import sys
from optparse import OptionParser

from dexen.system.common import service
from dexen.system.node import service as node_svc
from dexen.system.common.excepts import (DexenRegistryDiscoveryError,
                                         DexenConnectionError)


def start():
    parser = OptionParser("dexen-node [OPTIONS]... ")
    parser.add_option("-n", "--server-name", dest="reg_host", 
                      metavar="SERVER_NAME", default=service.get_my_ip(), 
                      help="Locate the server based on its name or ip.")
    parser.add_option("-s", "--num-slaves", dest="n_slave_slots", 
                      metavar="NUM_SLAVES", type="int", default=4, 
                      help="Start with NUM_SLAVES")
    parser.add_option("-v", "--verbose", dest="verbose", 
                      default=False, action="store_true", 
                      help="Print all the system messages.")
    
    (options, args) = parser.parse_args()
    if len(args) > 0:
        parser.error("Incorrect number of arguments.")
    print "Python Interpreter: %s"%(sys.executable)
    print "Dexen node is starting on %s."%(service.get_my_ip())
    is_quiet = not options.verbose
    try:
        node_svc.start(options.reg_host, options.n_slave_slots, is_quiet)
    except (DexenRegistryDiscoveryError, DexenConnectionError):
        err_msg = "Dexen node could not connect to the server [%s].\n"%(options.reg_host,) 
        err_msg += "Please check your network and firewall settings make sure "
        err_msg += "that the server is running."
        print err_msg

def main():
    start()
    
if __name__ == "__main__":
    main()  
